package com.core.zyyz.amc.modules.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.utils.StringUtils;
import com.core.zyyz.amc.modules.domain.AmcSystemConfigB;
import com.core.zyyz.amc.modules.mapper.AmcCodeManageMapper;
import com.core.zyyz.amc.modules.service.AmcSystemConfigBService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.zyyz.amc.modules.domain.AmcCodeManage;
import com.core.zyyz.amc.modules.service.AmcCodeManageService;
import com.core.common.core.page.TableDataInfo;

/**
 * 功能编号管理Controller
 *
 * @author LiuFushun
 * @date 2024-11-05
 */
@RestController
@RequestMapping("/amc/codeManage")
public class AmcCodeManageController extends BaseController
{
    @Autowired
    private AmcCodeManageService amcCodeManageService;
    @Autowired
    private AmcSystemConfigBService amcSystemConfigBService;
    @Autowired
    private AmcCodeManageMapper amcCodeManageMapper;

    /**
     * 查询功能编号管理列表
     */
    @PreAuthorize("@ss.hasPermi('amc:codeManage:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmcCodeManage amcCodeManage)
    {
        startPage();
        List<AmcCodeManage> list = amcCodeManageService.selectAmcCodeManageList(amcCodeManage);
        return getDataTable(list);
    }

    /**
     * 获取功能编号管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('amc:codeManage:query')")
    @GetMapping(value = "/{codeManageId}")
    public AjaxResult getInfo(@PathVariable("codeManageId") String codeManageId)
    {
        return success(amcCodeManageService.selectAmcCodeManageByCodeManageId(codeManageId));
    }

    /**
     * 新增功能编号管理
     */
    @PreAuthorize("@ss.hasPermi('amc:codeManage:add')")
    @Log(title = "功能编号管理", businessType = BusinessType.INSERT)
    @PostMapping("/insertCodeManage")
    public AjaxResult add(@RequestBody AmcCodeManage amcCodeManage) {
        boolean check = functionCheck(amcCodeManage);
        if(!check){
            return warn("功能编码已存在");
        }
        return toAjax(amcCodeManageService.insertAmcCodeManage(amcCodeManage));
    }

    /**
     * 修改功能编号管理
     */
    @PreAuthorize("@ss.hasPermi('amc:codeManage:edit')")
    @Log(title = "功能编号管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateCodeManage")
    public AjaxResult edit(@RequestBody AmcCodeManage amcCodeManage)
    {
        boolean check = functionCheck(amcCodeManage);
        if(!check){
            return warn("功能编号已存在，请确认编号!!!");
        }
        return toAjax(amcCodeManageService.updateAmcCodeManage(amcCodeManage));
    }

    /**
     * 校验：去重（code不能重复）
     * @param amcCodeManage
     */
    private boolean functionCheck(AmcCodeManage amcCodeManage) {
        int count =amcCodeManageMapper.selectCountFunctionCode(amcCodeManage);
        if(count > 0){
            return false;
        }
        return true;
    }
    
    /**
     * 删除功能编号管理
     */
    @PreAuthorize("@ss.hasPermi('amc:codeManage:remove')")
    @Log(title = "功能编号管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{codeManageIds}")
    public AjaxResult remove(@PathVariable String[] codeManageIds) {
        List<String> codeManageIdList = Arrays.asList(codeManageIds);
        for (String codeManageId : codeManageIdList){
            AmcCodeManage amcCodeManage = amcCodeManageService.selectAmcCodeManageByCodeManageId(codeManageId);
            if(!delFunctionCheck(amcCodeManage)){
                return warn("删除的数据中存在功能编号已被关联的情况，无法删除!!!");
            }
        }
        return toAjax(amcCodeManageService.deleteAmcCodeManageByCodeManageIds(codeManageIds));
    }

    /**
     * 修复数据字典值集配置
     */
    @PreAuthorize("@ss.hasPermi('amc:codeManage:refreshDict')")
    @Log(title = "修复数据字典值集配置", businessType = BusinessType.OTHER)
    @GetMapping("/refreshDict")
    @Transactional
    public AjaxResult refreshDict() {

        //功能设计复杂，需整理清楚后再完善
        return success();
    }

    /**
     * 校验：被关联过不可删除
     * @param amcCodeManage
     */
    private boolean delFunctionCheck(AmcCodeManage amcCodeManage) {
        int count = amcSystemConfigBService.selectAmcSystemConfigBFunctionCode(amcCodeManage.getFunctionCode());
        if(count > 0){
            return false;
        }
        return true;
    }
}
